package com.example.demo.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.SingleClause;
import com.example.demo.domain.entity.Company;
import com.example.demo.domain.entity.PermissionFrontUrl;
import com.example.demo.domain.entity.User;
import com.example.demo.enums.CompanyEnum;
import com.example.demo.exception.ValidatorException;
import com.example.demo.param.CompanyParam;
import com.example.demo.service.CompanyService;
import com.example.demo.mapper.CompanyMapper;
import com.example.demo.utils.ValidatorUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author DaoJia
* @description 针对表【company】的数据库操作Service实现
* @createDate 2024-05-04 23:06:42
*/
@Service
public class CompanyServiceImpl extends BaseServiceImpl<CompanyMapper, Company>
    implements CompanyService{

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public void createCompany(Company entity) {
        ValidatorUtil.require(entity.getTaxNo(), "税号");
        ValidatorUtil.require(entity.getName(), "公司名称");
        uniqueTaxNo(entity);
        entity.setStatus(CompanyEnum.status_one.getValue());
        companyMapper.insert(entity);
    }

    @Override
    public void updateCompany(Company entity) {
        ValidatorUtil.require(entity.getId(), "id");

        Company company = companyMapper.selectById(entity.getId());
        if (null == company){
            throw new ValidatorException("公司不存在");
        }

        if (!StringUtils.isEmpty(entity.getTaxNo())){
            if (!company.getTaxNo().equals(entity.getTaxNo())) {
                throw new ValidatorException("税号不能修改");
            }
        }

        companyMapper.updateById(entity);
    }

    @Override
    public void deleteCompany(Company entity) {
        ValidatorUtil.require(entity.getId(), "id");
        entity.setStatus(CompanyEnum.status_two.getValue());
        companyMapper.updateById(entity);
    }

    @Override
    public Page<Company> queryCompany(CompanyParam companyParam) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(companyParam.getName())){
            queryWrapper.like("name",companyParam.getName());
        }

        if(null != companyParam.getStatus()){
            queryWrapper.eq("status",companyParam.getStatus());
        }

        if(null!=companyParam.getId()){
            queryWrapper.eq("id",companyParam.getId());
        }
        queryWrapper.orderByDesc("id");

        Page<Company> page = new Page<>(companyParam.getPageNumber(), companyParam.getPageSize());
        return companyMapper.selectPage(page,queryWrapper);
    }


    private void uniqueTaxNo(Company entity){
        QueryWrapper<Company> queryWrapper = new QueryWrapper<Company>();
        queryWrapper.eq("tax_no", entity.getTaxNo());
        if (null != entity.getId()){
            queryWrapper.ne("id", entity.getId());
        }
        Company company = companyMapper.selectOne(queryWrapper);
        if (null != company) {
            throw new ValidatorException("税号已经存在");
        }
    }

    @Override
    public Map<Long, String> getNameMapByIds(List<Long> companyIds) {
        if (CollectionUtils.isEmpty(companyIds)) {
            return Collections.emptyMap();
        }
        return super.selectList(SingleClause.in("id", companyIds.toArray(new Object[0]))).stream()
                .collect(Collectors.toMap(Company::getId, Company::getName));
    }

}




